//Admin Login Form
function adminLoginForm(){
   	var flag=true;
	var useremail		=	$('#user_email').val();
	var userpassword	=	$('#user_password').val();
	
	if(useremail==""){
	   $("#user_emailError").html("Required");
	   flag=false;   
	}else{
	    if(validateEmail(useremail)==0){
		  $("#user_emailError").html("Valid Email");
		  flag=false; 
	    }else{
			  $("#user_emailError").html(""); 
			}
	}
	
	if(userpassword==""){
	   $("#user_passwordError").html("Required");
	   flag=false;   
	}else{
	    $("#user_passwordError").html(""); 
	} 
	if(flag==true){
		$('#loginloader').html('<img src="images/ajax-loader.gif"/>');
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'login',
			dataType	: 	"json",
			data		:	{user_email:useremail,user_password:userpassword,user_type_id:1},
			success: function(data){
				$('#loginloader').html('');
				if(data.value==1){	
					$('#loginError').html("");
					localStorage.setItem( 'admin_id', JSON.stringify(data.userData));			
					localStorage.setItem( 'email', JSON.stringify($('#user_email').val()));
					window.location="admin-dashboard.html";
				}else{
					$('#loginError').html("UserName/Password is wrong");
				}
			}
		});
	}
}
//Forget Password 

function forgetpasswordForm(){
    var flag=true;
	var user_email=$('#user_email').val();
	
	if(user_email==""){
	   $("#user_emailError").html("Required");
	   flag=false;   
	}else{
	    if(validateEmail(user_email)==0){
		  $("#user_emailError").html("Valid email");
		  flag=false; 
	    }else{
			  $("#user_emailError").html(""); 
			}
	}
	
	if(flag==true){
	   //$('#forgetReload').html('<img src="images/ajax-loader.gif"/>');
	   $.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'forgetpassword',
			dataType	: 	"json",
			data		:	{
								user_email:user_email
							},
			success: function(data){
			//$('#forgetReload').html('');
				if(data.value==1){
					$('#successfpassword').html(data.message);
				}else{
					$('#successfpassword').html(data.message);
				}
			}
		});
	}
}
//Reset password

function resetpasswordFunction(){
  var flag=true;
  var Resetpassword=$("#resetPassword").val();
  var Confirmpassword=$("#Confirmpassword").val();
    if(Resetpassword==""){
      $("#resetPasswordError").html("Required");
	  flag=false;
	}else{
      $("#resetPasswordError").html("");
	}
	
	if(Confirmpassword==""){
        $("#ConfirmpasswordError").html("Required");
		flag=false;
	}else if(Resetpassword!=Confirmpassword){
	   $("#ConfirmpasswordError").html("Not match");
	   flag=false;
	}else{
       $("#ConfirmpasswordError").html("");
	}
	
	if(flag==true){
	   var path = window.location.href;
	   path = path.replace(/\/$/, "");
	   path = decodeURIComponent(path);
       var id=path.split('=');
       var token=id[1];
	  // $('#resetPasswordReload').html('<img src="images/ajax-loader.gif"/>');
	    $.ajax({
				type		:	'PUT',
				url			:  	webServiceUrl+'forgetpassword/'+token,
				dataType	: 	"json",
				data        : {cnf_password:Confirmpassword},
				success: function(data){
					$('#resetPasswordReload').html('');
					if(data.value==1){
						$('#SuccessReset').html("Your password Update");
					}else{
						$('#SuccessReset').html(data.message);
					}
				}
		});
	}
}

// Email validation

function validateEmail(emailValue){
var emailvalidation = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   if(!emailvalidation.test(emailValue)){
     return 0;
    }else{
      return 1;
    }
}

function logoutFunction(){
	localStorage.removeItem("user_id");
	localStorage.removeItem("user_email");
	window.location="index.html";
}
// ADD course

function addCourse(){
   	var flag=true;
	var course_id=getParameterByName('id');
	var coursename		=	$('#course_name').val();
	
	if(coursename==""){
	   $("#course_nameError").html("Required");
	   flag=false;   
	}else{
		$("#course_nameError").html(""); 
	}
	if(flag==true){
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'addcourses',
			dataType	: 	"json",
			data		:	{course_name:coursename,hid_course_id:course_id},
			success: function(data){
				if(data.value==1){	
					$('#loginError').html("");
					localStorage.setItem( 'course_id', JSON.stringify(data.courseId));		
					window.location="courselist.html";
				}
			}
		});
	}
}
//BACk course 
function backCourse(){
	window.location="courselist.html";
}
//Add subject
function adSubjectFunction(){
	window.location="addsubjects.html";
}
//Add subject

function addSubject(){
	//var course_id = JSON.parse(localStorage["course_id"]);
	var flag=true;
	var SubId=getParameterByName('id');
	var course_id=$("#course_name").val();
	var sunbjectName=$("#subject_name").val();
	if(course_name==""){
		$("#course_nameError").html('Required');
		flag=false;
	}else{
		$("#course_nameError").html('');
	}
	if(sunbjectName==""){
		$("#subject_nameError").html('Required');
		flag=false;
	}else{
		$("#subject_nameError").html('');
	}
	if(flag==true){
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'addsubjects',
			dataType	: 	"json",
			data		:	{course_id:course_id,subject_name:sunbjectName,SubId:SubId},
			success: function(data){
				if(data.value==1){	
					localStorage.setItem( 'subject_id', JSON.stringify(data.subjectId));	
					window.location="subjectslist.html";
				}
			}
		});
	}
}
//Add technology
function addTechnology(){
	//var course_id = JSON.parse(localStorage["course_id"]);
	var flag=true;
	var technologyName=$("#technology_name").val();
	var technologyDescription=$("#tech_description").val();
	var technologyimage=$("#techimage").val();
	var hid_val=$('#hid_tech_val').val();
	var hidimage=$('#hidimage').val();
	var hid_dis_image=$('#hid_display_image').val();
	var hid_technology_id=$('#hid_technology_id').val();
	//var image=$('#display_Image').val();
	if(technologyName==""){
		$("#technology_nameError").html('Required');
		flag=false;
	}else{
		$("#technology_nameError").html('');
	}
	if(technologyDescription==""){
		$("#tech_descriptionError").html('Required');
		flag=false;
	}else{
		$("#tech_descriptionError").html('');
	}
	/*if(technologyimage==""){
		$("#techimageError").html('Required');
		flag=false;
	}else{
		$("#techimageError").html('');
	}*/
	if(flag==true){
	$('#techloader').html('<img src="images/ajax-loader.gif"/>');
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'addtechnology',
			dataType	: 	"json",
			data		:	{technology_name:technologyName,tech_description:technologyDescription,hidimage:hidimage,hid_tech_val:hid_val,hid_dis_image:hid_dis_image,hid_technology_id:hid_technology_id},
			success: function(data){
			$('#techloader').html('');
				if(data.value==1){	
					localStorage.setItem( 'technology_id', JSON.stringify(data.technologyId));	
					window.location="technologylist.html";
				}
			}
		});
	}
}
// Back Subject
function backSubject(){
	window.location="subjectslist.html";
}
//ADD Questions 

function addQuestion(){
	var type=getParameterByName('type');
	var splitquestion_id=getParameterByName('id');
	var splitIds=splitquestion_id.split('-');
	if(splitIds[2]==undefined){
		var hid_question_id=$('#hid_question_id').val();
	}else{
		var hid_question_id=splitIds[2];
	}
   var flag=true;
	
	var courseName		=	$('#course_name').val();
	var subjectName		=	$('#subject_name').val();
	var questionName	=	$('#question_name').val();
	var option1			=	$('#option1').val();
	var option2			=	$('#option2').val();
	var option3			=	$('#option3').val();
	var option4 		=	$('#option4').val();
	var correctAnswer 	=	$('#correct_answer').val();
	var level 			=	$('#level').val();
	var year_of_exp 	=	$('#year_of_experience').val();
	
	if(courseName==""){
	   $("#course_nameError").html("Required");
	   flag=false;   
	}else{
	   $("#course_nameError").html(""); 
	}
	if(subjectName==""){
	   $("#subject_nameError").html("Required");
	   flag=false;   
	}else{
	   $("#subject_nameError").html(""); 
	}
	
	if(questionName==""){
	   $("#question_nameError").html("Required");
	   flag=false;   
	}else{
	   $("#question_nameError").html(""); 
	}
	
	/*if(option1==""){
			$("#option1Error").html("Required");
			flag=false;   
	}else{
		$("#option1Error").html(""); 
	} 
	if(option2==""){
	   $("#option2Error").html("Required");
	   flag=false;   
	}else{
		$("#option2Error").html(""); 
	} 
	if(option3==""){
	   $("#option3Error").html("Required");
	   flag=false;   
	}else{
		$("#option3Error").html(""); 
	} 
	if(option4==""){
	   $("#option4Error").html("Required");
	   flag=false;   
	}else{
		$("#option4Error").html(""); 
	} */
	if(correctAnswer=="" ){
	   $("#correct_answerError").html("Required");
	   flag=false;   
	}else if(correctAnswer!='option1' && correctAnswer!='option2' && correctAnswer!='option3' && correctAnswer!='option4'){
		 $("#correct_answerError").html("Please enter correct answer(option1||option2||option3)");
		  flag=false;   
	}else{
	    $("#correct_answerError").html(""); 
	} 
	
	if(level==""){
	   $("#levelError").html("Required");
	   flag=false;   
	}else{
	    $("#levelError").html(""); 
	} 
	if(year_of_exp==""){
	   $("#year_of_experienceError").html("Required");
	   flag=false;   
	}else{
	    $("#year_of_experienceError").html(""); 
	} 
	if(flag==true){
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'addquestions',
			dataType	: 	"json",
			data		:	{course_id:courseName,subject_id:subjectName,question_name:questionName,option1:option1,option2:option2,option3:option3,option4:option4,correct_answer:correctAnswer,level:level,year_of_experience:year_of_exp,question_id:hid_question_id,type:type},
			success: function(data){
				if(data.value==1){	
					localStorage.setItem( 'question_id', JSON.stringify(data.questionId));
					if(type==0){
						window.location="interview-questionslist.html?id="+'0';
					}else{
						window.location="questionslist.html";
					}
					
				}
			}
		});
	}
}
function adminDashboardFunction(type){
	if(type==1){
		window.location="adminlist.html";
	}else if(type==2){
		window.location="userlist.html";
	}else if(type==3){
		window.location="interview-questionslist.html?id="+'0';
	}else if(type==4){
		window.location="questionslist.html";
	}else if(type==5){
		window.location="courselist.html";
	}else if(type==6){
		window.location="subjectslist.html";
	}else if(type==7){
		window.location="technologylist.html";
	}else{
		window.location="changepassword.html";
	}	
}

function addcourseFunction(){
	window.location="addcourses.html";
}
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Back function

function backFunction(){
	window.location="admin-dashboard.html";
}

function addTechnologyFunction(){
	window.location="addtechnology.html";
}
function backTechnology(){
window.location="technologylist.html";
}
function backQuestion(){
var type=getParameterByName('type');
	if(type==0){
		window.location="interview-questionslist.html?id="+'0';
	}else{
		window.location="questionslist.html";
	}
}
function addQuestionFunction(type){
window.location="addquestions.html?type="+type;
}
//User Delete
function deleteuser(user_id){
$.ajax({
		type		:	'DELETE',
		url			:  	webServiceUrl+'login/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				alert("User Delete Successfully.");
				window.location="userlist.html";
			}
		}
	});
}
// course 
function deleteCourse(course_id){
$.ajax({
		type		:	'DELETE',
		url			:  	webServiceUrl+'addcourses/'+course_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				alert("Course Delete Successfully.");
				window.location="courselist.html";
			}
		}
	});
}
//Subject
function deleteSubject(sub_id){
$.ajax({
		type		:	'DELETE',
		url			:  	webServiceUrl+'addsubjects/'+sub_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				alert("Subject Delete Successfully.");
				window.location="subjectslist.html";
			}
		}
	});
}

//Techonology
function deleteTechnology(tech_id){
$.ajax({
		type		:	'DELETE',
		url			:  	webServiceUrl+'addtechnology/'+tech_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				alert("Technology Delete Successfully.");
				window.location="technologylist.html";
			}
		}
	});
}

//Qustion 

function deleteQuestion(sub_id,c_id,que_id){
var type=getParameterByName('type');
$.ajax({
		type		:	'DELETE',
		url			:  	webServiceUrl+'addquestions/'+sub_id+'-'+c_id+'-'+que_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				
				if(type==0){
					alert("InterviewQuestion Delete Successfully.");
					window.location="interview-questionslist.html?id="+'0';
				}else{
					alert("Question Delete Successfully.");
					window.location="questionslist.html";
				}
			}
		}
	});
}